<?php

namespace App\Modules\Admin\Entities\Member;
use App\Models\Model;
use App\Modules\Admin\Entities\Vip\VipModel;
use Illuminate\Database\Eloquent\Relations\HasOne;

class UsersModel extends Model
{
    protected $table = "users";
    protected $primaryKey='id';



    public function usersIdCard()
    {
        return $this->hasOne(UsersIdCardModel::class, 'user_id','id');
    }
    /**
     * @return HasOne
     */
    public function role(): HasOne
    {
        return $this->hasOne(UsersRoleModel::class,'id','role');
    }
    /**
     * @return HasOne
     */
    public function user_card(): HasOne
    {
        return $this->hasOne(UserCardModel::class,'user_id','id');
    }
    /**
     * @return HasOne
     */
    public function vip(): HasOne
    {
        return $this->hasOne(VipModel::class,'id','vip_id');
    }

    /**
     * @return
     */
    public function getUserInfo($id,$field = '*')
    {
        if (is_string($field)) $field = explode(',', $field);
        return $this->where(['id'=>$id])->first($field);
    }
    /**
     * @return
     */
    public function scoreIncrement($id,$integral)
    {
        return $this->where(['id'=>$id])->increment('integral',number_format($integral,3));
    }
    public function scoreDecrement($id,$integral)
    {
        return $this->where(['id',$id])->decrement('integral',number_format($integral,3));
    }

    public function userWaterCard()
    {
        return $this->belongsTo(UserWaterCardModel::class,'user_id','user_id');
    }

    public function spread_device()
    {
        return $this->belongsTo(UserWaterCardModel::class,'user_id','user_id');
    }



}
